package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.query.TravelCommendQuery;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.SevenUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/travelCommend")
public class TravelCommendController {
    @Autowired
    private ITravelCommendService travelCommendService;
    @Autowired
    private ITravelService travelService;
    //待发布状态的游记管理
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo")TravelCommendQuery qo, Model model){
        qo.setOrder("tm.schedule desc");
        model.addAttribute("result",travelCommendService.query(qo));
        return "travelCommend/list";
    }
    //保存或者修改推荐游记
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(TravelCommend travelCommend, MultipartFile file){
        JSONResult jsonResult = new JSONResult();
        try {
            //保存照片
            if (file !=null && file.getSize()>0){
                String filePath = SevenUploadUtil.uploadQiniu(file);
                travelCommend.setCoverUrl(filePath);
            }
            travelCommendService.saveOrUpdate(travelCommend);
        }catch (Exception e){
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }
    //取消推荐操作
    @RequestMapping("/cancel")
    @ResponseBody
    public Object delete(Long id,Long travelId){
        JSONResult jsonResult = new JSONResult();
        try {
            //删除推荐
           travelCommendService.deleteById(id);
        }catch (Exception e){
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }
}
